package org.andao.commons.taglib.security;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;

import org.andao.commons.taglib.BaseTagSupport;
import org.andao.core.support.context.SpringContextHolder;
import org.andao.core.utils.ExCollectionUtils;
import org.andao.core.utils.ExStringUtils;
import org.andao.security.SpringSecurityHelper;
import org.andao.security.cache.CacheSecManager;
import org.andao.security.model.Resource;
import org.andao.security.model.User;
import org.andao.security.service.ResourceService;


/**
 * seewo 教育云 - 老师中心菜单.
 *
 * @author Seewo Software - Marco.hu(huzhiguo@cvte.cn)
 *
 */
public class CurrentUserForSeewoMenuTag  extends BaseTagSupport{
	
	private static final long serialVersionUID = -8141176120403836979L;

	private String parentCode;
	private String menuType = "top";
	private String currentCode;
	private String pageType;
	
	@Override
	public int doStartTag() throws JspException {
		User user = SpringSecurityHelper.getCurrentUser();
		StringBuilder sb = new StringBuilder();
		if(menuType.equals("top")){//顶部菜单
			
		}else if(menuType.equals("left")){//左侧菜单
			sb.append("<div id=\"left_nav\">	");
			if(ExStringUtils.isNotBlank(parentCode)){
				ResourceService resourceService = (ResourceService)SpringContextHolder.getBean("resourceService");
				Resource currentRes = resourceService.findUniqueByProperty("resourceCode", parentCode);				
				if(ExCollectionUtils.isNotEmpty(currentRes.getChildren())){				
					for(Resource res : currentRes.getChildren()){					
						if( user.getUserRightsIds().indexOf(res.getResourceid())<0) continue;
						sb.append("<a id='"+res.getResourceDescript()+"' ");
						if(ExStringUtils.isNotBlank(currentCode) && currentCode.equals(res.getResourceCode()))
							sb.append(" class=\"teach\" ") ;
						sb.append(" href='"+getBaseUrl()+ res.getResourcePath()+"'>"+res.getResourceName()+"</a>");
						
					}
				}
			}
			sb.append("</div>");
			
		}else{//功能点
			
		}
		try {
			this.pageContext.getOut().append(sb.toString());
		} catch (IOException e) {		
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	public String getParentCode() {
		return parentCode;
	}

	public void setParentCode(String parentCode) {
		this.parentCode = parentCode;
	}

	public String getPageType() {
		return pageType;
	}

	public void setPageType(String pageType) {
		this.pageType = pageType;
	}

	public String getMenuType() {
		return menuType;
	}

	public void setMenuType(String menuType) {
		this.menuType = menuType;
	}

	public String getCurrentCode() {
		return currentCode;
	}

	public void setCurrentCode(String currentCode) {
		this.currentCode = currentCode;
	}
	
	
}
